//
//  LXDChartsViewController.m
//  BLDMusics
//
//  Created by lxd on 16/3/17.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDChartsViewController.h"

@interface LXDChartsViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) UITableView *tableView;


@property (nonatomic, strong) NSMutableArray *modelArr;

/**小菊花*/
@property (nonatomic, retain) UIView *hudView;

@end

@implementation LXDChartsViewController

- (void)viewWillAppear:(BOOL)animated {
    [PlayViewController sharePlayViewBar].hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

   self.navigationItem.title = @"排行榜";
    
    [self createTableView];
    [self dataHandle];
}



- (void)createTableView {
    self.modelArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 64 - 50) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 行高
    _tableView.rowHeight = (SCREEN_WIDTH - 30) / 3 + 20;
    
    
    // 注册
    [_tableView registerClass:[LXDChartsTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    /**小菊花*/
    self.hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
    _hudView.center = CGPointMake(self.view.center.x, _tableView.frame.size.height / 2);
    [self.view addSubview:_hudView];

}


#pragma mark - tableView的协议方法
#pragma mark 每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    LXDChartsModel *model = _modelArr[section];
    return model.refsArray.count;
}
#pragma mark 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _modelArr.count;
}
#pragma mark 返回分区标题
// 设置分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    LXDChartsModel *model = _modelArr[section];
    return model.name;
}
#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LXDChartsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    LXDChartsModel *model = _modelArr[indexPath.section];
    cell.model = model.refsArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LXDChartsModel *model = _modelArr[indexPath.section];
    LXDChartsModel *model1 = model.refsArray[indexPath.row];
    if (model1.link.length) {
        WEBViewController *vc = [[WEBViewController alloc] init];
        vc.url = model1.link;
        [PlayViewController sharePlayViewBar].hidden = YES;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        BYJCarouselViewController *vc = [[BYJCarouselViewController alloc] init];
        vc.value = model1.songlist_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - 数据解析
- (void)dataHandle {
    [MBProgressHUD showHUDAddedTo:_hudView animated:YES];
    NSString *interface = @"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=921fb117b843753583dbf257222bb96a79d3c64e&idfa=5B1500CB-A041-4C95-951A-F6408B90A0BF&utdid=Vule3TyuSLwDAKd%2FfPxZrXTy&alf=201200&bundle_id=com.ttpod.music&latitude=38.88269196885403&longtitude=121.539207307381";
    [APPTools GETWithURL:interface par:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:_hudView animated:YES];
        [_hudView removeFromSuperview];
        self.modelArr = [LXDChartsModel modelHandleWithArray:responseObject];
        [_tableView reloadData];
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
